package com.info.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.entity.PieChartsData;
import com.entity.ZichanjiechuEntity;
import com.entity.ZichanxinxiEntity;
import com.entity.view.ZichanxinxiView;
import com.feign.BorrowFeignService;
import com.feign.TokenFeignService;
import com.info.service.RedisService;
import com.info.service.impl.ZichanxinxiService;
import com.utils.MPUtil;
import com.utils.PageUtils;
import com.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;


/**
 * 资产信息
 * 后端接口
 *
 * @author
 * @email
 */
@RestController
@RequestMapping("/zichanxinxi")
public class ZichanxinxiController {
    @Autowired
    private ZichanxinxiService zichanxinxiService;

    @Autowired
    private TokenFeignService tokenFeignService;

    @Autowired
    private BorrowFeignService borrowFeignService;
    @Resource
    private RedisService redisService;

    /**
     * 后端列表
     */
    @RequestMapping("/page")
    public R page(@RequestParam Map<String, Object> params, ZichanxinxiEntity zichanxinxi,
                  HttpServletRequest request) {
        EntityWrapper<ZichanxinxiEntity> ew = new EntityWrapper<ZichanxinxiEntity>();
        PageUtils page = zichanxinxiService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, zichanxinxi), params), params));

        return R.ok().put("data", page);
    }

    /**
     * 前端列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params, ZichanxinxiEntity zichanxinxi,
                  HttpServletRequest request) {
        EntityWrapper<ZichanxinxiEntity> ew = new EntityWrapper<ZichanxinxiEntity>();
        PageUtils page = zichanxinxiService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, zichanxinxi), params), params));
        return R.ok().put("data", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/lists")
    public R list(ZichanxinxiEntity zichanxinxi) {
        EntityWrapper<ZichanxinxiEntity> ew = new EntityWrapper<ZichanxinxiEntity>();
        ew.allEq(MPUtil.allEQMapPre(zichanxinxi, "zichanxinxi"));
        return R.ok().put("data", zichanxinxiService.selectListView(ew));
    }

    /**
     * 查询
     */
    @RequestMapping("/query")
    public R query(ZichanxinxiEntity zichanxinxi) {
        EntityWrapper<ZichanxinxiEntity> ew = new EntityWrapper<ZichanxinxiEntity>();
        ew.allEq(MPUtil.allEQMapPre(zichanxinxi, "zichanxinxi"));
        ZichanxinxiView zichanxinxiView = zichanxinxiService.selectView(ew);
        return R.ok("查询资产信息成功").put("data", zichanxinxiView);
    }

    /**
     * 后端详情
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        ZichanxinxiEntity zichanxinxi = (ZichanxinxiEntity) redisService.get("Zichanxinxi" + id);
        if (zichanxinxi == null) {
            zichanxinxi = zichanxinxiService.selectById(id);
            redisService.set("Zichanxinxi" + id, zichanxinxi);
        }
        return R.ok().put("data", zichanxinxi);
    }

    /**
     * 前端详情
     */
    @RequestMapping("/detail/{id}")
    public R detail(@PathVariable("id") Long id) {
        ZichanxinxiEntity zichanxinxi = (ZichanxinxiEntity) redisService.get("Zichanxinxi" + id);
        if (zichanxinxi == null) {
            zichanxinxi = zichanxinxiService.selectById(id);
            redisService.set("Zichanxinxi" + id, zichanxinxi);
        }
        return R.ok().put("data", zichanxinxi);
    }


    /**
     * 后端保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody ZichanxinxiEntity zichanxinxi, HttpServletRequest request) {
        zichanxinxi.setId(new Date().getTime() + new Double(Math.floor(Math.random() * 1000)).longValue());
        //ValidatorUtils.validateEntity(zichanxinxi);
        zichanxinxiService.insert(zichanxinxi);
        redisService.set("Zichanxinxi" + zichanxinxi.getId(), zichanxinxi);

        return R.ok();
    }

    /**
     * 前端保存
     */
    @RequestMapping("/add")
    public R add(@RequestBody ZichanxinxiEntity zichanxinxi, HttpServletRequest request) {
        zichanxinxi.setId(new Date().getTime() + new Double(Math.floor(Math.random() * 1000)).longValue());
        //ValidatorUtils.validateEntity(zichanxinxi);
        zichanxinxiService.insert(zichanxinxi);
        redisService.set("Zichanxinxi" + zichanxinxi.getId(), zichanxinxi);

        return R.ok();
    }

    /**
     * 修改
     * <p>
     * author : yue
     */
    @RequestMapping("/update")
    public R update(@RequestBody ZichanxinxiEntity zichanxinxi, HttpServletRequest request) {
        //ValidatorUtils.validateEntity(zichanxinxi);
        zichanxinxiService.updateById(zichanxinxi);
        redisService.set("Zichanxinxi" + zichanxinxi.getId(), zichanxinxi);

        return R.ok();
    }


    //把未审核数量加回来
    @GetMapping("/updatesl")
    public R update(@RequestParam("zcbh") String zcbh, @RequestParam("sl") Integer sl) {
        //ValidatorUtils.validateEntity(zichanxinxi);
        EntityWrapper<ZichanxinxiEntity> ew = new EntityWrapper<ZichanxinxiEntity>();
        Wrapper<ZichanxinxiEntity> zichanbianhao = ew.eq("zichanbianhao", zcbh);
        ZichanxinxiEntity zichanxinxiEntity = zichanxinxiService.selectOne(zichanbianhao);
        zichanxinxiEntity.setShuliang(zichanxinxiEntity.getShuliang() + sl);
        zichanxinxiService.updateById(zichanxinxiEntity);//全部更新
        redisService.set("Zichanxinxi" + zichanxinxiEntity.getId(), zichanxinxiEntity);

        return R.ok();
    }


    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        zichanxinxiService.deleteBatchIds(Arrays.asList(ids));
        for (Long id : ids) {
            zichanxinxiService.deleteById("Zichanxinxi" + id);
        }
        return R.ok();
    }

    /**
     * 资产饼图
     */
    @GetMapping("/getzcpie")
    public R getzcpie() {
        List<PieChartsData> list = new ArrayList<>();
        List<ZichanxinxiEntity> zichanxinxiEntities = zichanxinxiService.selectList(null);

        Map<String, Long> groupedByNameCount = zichanxinxiEntities.stream()
                .collect(Collectors.groupingBy(ZichanxinxiEntity::getZichanfenlei, Collectors.counting()));
        groupedByNameCount.forEach((k, v) -> {
            PieChartsData pieChartsData = new PieChartsData();
            pieChartsData.setName(k);
            pieChartsData.setValue(v.intValue());
            list.add(pieChartsData);
        });
        return R.ok().put("list", list);
    }

    /**
     * 提醒接口
     */
    @RequestMapping("/remind/{columnName}/{type}")
    public R remindCount(@PathVariable("columnName") String columnName, HttpServletRequest request,
                         @PathVariable("type") String type, @RequestParam Map<String, Object> map) {
        map.put("column", columnName);
        map.put("type", type);

        if (type.equals("2")) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Calendar c = Calendar.getInstance();
            Date remindStartDate = null;
            Date remindEndDate = null;
            if (map.get("remindstart") != null) {
                Integer remindStart = Integer.parseInt(map.get("remindstart").toString());
                c.setTime(new Date());
                c.add(Calendar.DAY_OF_MONTH, remindStart);
                remindStartDate = c.getTime();
                map.put("remindstart", sdf.format(remindStartDate));
            }
            if (map.get("remindend") != null) {
                Integer remindEnd = Integer.parseInt(map.get("remindend").toString());
                c.setTime(new Date());
                c.add(Calendar.DAY_OF_MONTH, remindEnd);
                remindEndDate = c.getTime();
                map.put("remindend", sdf.format(remindEndDate));
            }
        }

        Wrapper<ZichanxinxiEntity> wrapper = new EntityWrapper<ZichanxinxiEntity>();
        if (map.get("remindstart") != null) {
            wrapper.ge(columnName, map.get("remindstart"));
        }
        if (map.get("remindend") != null) {
            wrapper.le(columnName, map.get("remindend"));
        }


        int count = zichanxinxiService.selectCount(wrapper);
        return R.ok().put("count", count);
    }


}
